package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.StrategyQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVo;
import cn.wolfcode.luowowo.website.annotation.LoginRequire;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyEsService strategyEsService;


    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends",commends);

        //hotCds
        List<StrategyStatisVO> hotCds = strategyStatisVOService.queryHotSortTop10();
        model.addAttribute("hotCds",hotCds);

        //abroadCds  国外排行前十攻略
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.queryAbroadCdsSortTop10();
        model.addAttribute("abroadCds",abroadCds);
        //unabroadCds  国内排行前十攻略
        List<StrategyStatisVO> unabroadCds = strategyStatisVOService.queryUnabroadCdsSortTop10();
        model.addAttribute("unabroadCds",unabroadCds);

        //themeChs  主题推荐
        //优化点：
        // 1.目的地个数排序
        // 2.取前面10个
        // 3.目的地重复
        List<Map<String,Object>> data = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds",data);

        //chinas
        List<StatisVo> chinas = strategyEsService.queryConditionChaina();
        model.addAttribute("chinas",chinas);


        //abroads
        List<StatisVo> abroads = strategyEsService.queryConditionAbroads();
        model.addAttribute("abroads",abroads);


        //themes
        List<StatisVo> themes = strategyEsService.queryConditionThemes();
        model.addAttribute("themes",themes);

        return "strategy/index";

    }


    @RequestMapping("/detail")
    public String detail(Model model,Long id){

        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        //阅读数+1
        strategyStatisVOService.increaseViewNum(id,1);
        //获取到攻略的统计对象再共享出去
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(id);
        model.addAttribute("vo",vo);

        //热们攻略排行：阅读加分
        strategyStatisVOService.addScoreHot(id,1);

        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") StrategyDetailQuery qo){

        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        //dest
        model.addAttribute("dest",dest);
        //tags
        model.addAttribute("tags",strategyTagService.list());
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(Model model,StrategyComment comment, @UserParam UserInfo userInfo){


        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        strategyCommentService.save(comment);

        //添加评论数
        strategyStatisVOService.increaseReplyNum(comment.getDetailId(),1);
        //获取到评论数
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(comment.getDetailId());
        //热们攻略排行：评论加分
        strategyStatisVOService.addScoreHot(comment.getDetailId(),1);

        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    @RequestMapping("/comment")
    public String comment(@ModelAttribute("qo") StrategyCommentQuery qo, Model model){

        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    @LoginRequire
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,@UserParam UserInfo userInfo,Model model){

        strategyCommentService.commentThumbUp(toid,userInfo.getId());
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);

            return result;
        }
        boolean flag = strategyStatisVOService.increaseFavorNum(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);


        /*if(flag){
            //国内外攻略排行：收藏加分
            strategyStatisVOService.addScoreForeignOrChina(sid,1);
        }else{
            //国内外攻略排行：取消收藏减分
            strategyStatisVOService.addScoreForeignOrChina(sid,-1);
        }*/

        //国内外攻略排行：收藏加分  取消收藏减分
        strategyStatisVOService.addScoreForeignOrChina(sid,flag?1:-1);


        return new AjaxResult(flag,"").addData(vo);
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if(userInfo == null){
            //没登录
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        //得到true或false  判断今天是否已经点赞过了
        boolean flag = strategyStatisVOService.thumbup(sid, userInfo.getId());
        //获得到vo攻略统计对象然后响应给前端
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(sid);

        //国内外攻略排行：点赞加分
        if(flag) strategyStatisVOService.addScoreForeignOrChina(sid,1);

        return new AjaxResult(flag,"").addData(vo);
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){

        Page page = strategyEsService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }


}
